/* hooks */
import { useBaseSelectUserStoreFilterForm } from '@src/component/common/BaseSelectUser/store/hooks/filter'
import { useBaseSelectUserOptions } from '@src/component/common/BaseSelectUser/hooks/options'
/* model */
import { BaseSelectUserSearchMoreTypeEnum } from '@src/component/common/BaseSelectUser/model'
/* store */
import {
  useBaseSelectUserStoreCurrentDepartmentNav,
  useBaseSelectUserStoreCurrentToggleRole,
  useBaseSelectUserStoreDepartmentListParams,
  useBaseSelectUserStoreDepartmentUserListParams,
  useBaseSelectUserStoreRoleUserListParams,
  useBaseSelectUserStoreSearchMoreUserListParams,
  useBaseSelectUserStoreServiceProviderUserListParams,
  useBaseSelectUserStoreCurrentSearchMoreType,
  useBaseSelectUserStoreSearchKeyword,
  useBaseSelectUserStoreDepartmentData,
  useBaseSelectUserStoreCurrentServiceProviderNav,
} from '@src/component/common/BaseSelectUser/store'
/* vue */
import { computed, ComputedRef } from 'vue'
/* types */
import { 
  BaseSelectUserGetDepartmentListModel,
  BaseSelectUserGetDepartmentUserListModel,
  BaseSelectUserGetRoleListModel, 
  BaseSelectUserGetRoleUserListModel, 
  BaseSelectUserGetSearchMoreUserListModel, 
  BaseSelectUserGetServiceProviderListModel, 
  BaseSelectUserGetServiceProviderUserListModel
} from '@src/component/common/BaseSelectUser/types'
/* util */
import { stringIdListToNumberIdList, getListMainValue } from '@src/component/common/BaseSelectUser/util'
import { isBoolean } from '@src/util/type'

/** 
 * @description 选人组件 部门列表参数
*/
function useBaseSelectUserDepartmentListParams() {
  
  const { departmentListParams: originDepartmentListParams, setDepartmentListParams } = useBaseSelectUserStoreDepartmentListParams()
  const { filterDepartmentForm } = useBaseSelectUserStoreFilterForm()
  const { showDelete, isShowUser, isTag, isAuthKey } = useBaseSelectUserOptions()
  
  const departmentUserListParams: ComputedRef<BaseSelectUserGetDepartmentListModel> = computed(() => {
    
    let params: BaseSelectUserGetDepartmentListModel = {
      ...filterDepartmentForm.value,
      // 页码数
      pageNum: originDepartmentListParams.value.pageNum,
      // 页大小
      pageSize: originDepartmentListParams.value.pageSize,
      // 是否显示已删除
      showDelete: showDelete.value,
      // 是否显示用户列表
      showUser: isShowUser.value,
      // 区分全部和团队权限
      authKey: isAuthKey.value,
      qualificationIds:getListMainValue(filterDepartmentForm.value.qualificationManagement)
    }
    
    if (isBoolean(isTag.value)) {
      params.isTag = isTag.value
    }
    
    return params
  })
  
  const setPageNum = (pageNum: number = 1) => {
    
    const newDepartmentListParams = {
      ...originDepartmentListParams.value,
      pageNum
    }
    
    setDepartmentListParams(newDepartmentListParams)
  }
  
  const addPageNum = () => {
    const nextPageNum = originDepartmentListParams.value.pageNum + 1
    setPageNum(nextPageNum)
  }
  
  const resetPageNum = () => {
    setPageNum()
  }
  
  return {
    departmentUserListParams,
    addPageNum,
    resetPageNum
  }
}

/** 
 * @description 选人组件 部门人员列表参数
*/
function useBaseSelectUserDepartmentUserListParams() {
  
  const { departmentUserListParams: originDepartmentUserListParams, setDepartmentUserListParams } = useBaseSelectUserStoreDepartmentUserListParams()
  const { filterDepartmentForm } = useBaseSelectUserStoreFilterForm()
  const { currentDepartmentNav } = useBaseSelectUserStoreCurrentDepartmentNav()
  const { isShowUser, showNoDistribute } = useBaseSelectUserOptions()
  
  const departmentUserListParams: ComputedRef<BaseSelectUserGetDepartmentUserListModel> = computed(() => {
    return {
      // 部门 id
      id: currentDepartmentNav.value?.id || '',
      // 类型 id, 用于区分是团队还是部门
      typeId: currentDepartmentNav.value?.typeId || null,
      // 工作状态 列表
      stateList: filterDepartmentForm.value.stateList,
      // 人员标签 id 列表
      labelIdList: stringIdListToNumberIdList(filterDepartmentForm.value.labelIdList),
      // 页码数
      pageNum: originDepartmentUserListParams.value.pageNum,
      // 页大小
      pageSize: originDepartmentUserListParams.value.pageSize,
      // 是否显示用户列表
      showUser: isShowUser.value,
      // 是否显示未分配人员
      showNoDistribute: showNoDistribute.value,
      qualificationIds:getListMainValue(filterDepartmentForm.value.qualificationManagement)
    }
  })
  
  const setPageNum = (pageNum: number = 1) => {
    
    const newDepartmentUserListParams = {
      ...originDepartmentUserListParams.value,
      pageNum
    }
    
    setDepartmentUserListParams(newDepartmentUserListParams)
  }
  
  const addPageNum = () => {
    const nextPageNum = originDepartmentUserListParams.value.pageNum + 1
    setPageNum(nextPageNum)
  }
  
  const resetPageNum = () => {
    setPageNum()
  }
  
  return {
    departmentUserListParams,
    addPageNum,
    resetPageNum
  }
}

/** 
 * @description 选人组件 角色列表参数
*/
function useBaseSelectUserRoleListParams() {
  
  const { filterRoleForm } = useBaseSelectUserStoreFilterForm()
  const { showNoDistribute } = useBaseSelectUserOptions()
  
  const roleListParams: ComputedRef<BaseSelectUserGetRoleListModel> = computed(() => {
    return {
      ...filterRoleForm.value,
      // 是否显示 未分配
      showNoDistribute: showNoDistribute.value
    }
  })
  
  return {
    roleListParams
  }
}

/** 
 * @description 选人组件 角色人员列表参数
*/
function useBaseSelectUserRoleUserListParams() {
  
  const { roleUserListParams: originRoleUserListParams, setRoleUserListParams } = useBaseSelectUserStoreRoleUserListParams()
  const { filterRoleForm } = useBaseSelectUserStoreFilterForm()
  const { currentToggleRole } = useBaseSelectUserStoreCurrentToggleRole()
  
  const roleUserListParams: ComputedRef<BaseSelectUserGetRoleUserListModel> = computed(() => {
    return {
      // 角色 id
      id: currentToggleRole.value?.id || '',
      // 工作状态 列表
      stateList: filterRoleForm.value.stateList,
      // 人员标签 id 列表
      labelIdList: stringIdListToNumberIdList(filterRoleForm.value.labelIdList),
      // 页码数
      pageNum: originRoleUserListParams.value.pageNum,
      // 页大小
      pageSize: originRoleUserListParams.value.pageSize,
      qualificationIds:getListMainValue(filterRoleForm.value.qualificationManagement)
    }
  })
  
  const setPageNum = (pageNum: number = 1) => {
    
    const newRoleUserListParams = {
      ...originRoleUserListParams.value,
      pageNum
    }
    
    setRoleUserListParams(newRoleUserListParams)
  }
  
  const addPageNum = () => {
    const nextPageNum = originRoleUserListParams.value.pageNum + 1
    setPageNum(nextPageNum)
  }
  
  const resetPageNum = () => {
    setPageNum()
  }
  
  return {
    roleUserListParams,
    addPageNum,
    resetPageNum
  }
}

/** 
 * @description 选人组件 服务商列表参数
*/
function useBaseSelectUserServiceProviderListParams() {
  
  const { filterServiceProviderForm } = useBaseSelectUserStoreFilterForm()
  const { showCancel, showSpAll, isAuthKey, isHideNullProvider } = useBaseSelectUserOptions()
  
  const serviceProviderListParams: ComputedRef<BaseSelectUserGetServiceProviderListModel> = computed(() => {
    return {
      ...filterServiceProviderForm.value,
      // 是否显示已注销
      showCancel: showCancel.value,
      // 是否显示服务商全部
      showSpAll: showSpAll.value,
      // 区分全部和团队权限
      authKey: isAuthKey.value,
      // 服务商是否隐藏没有工程师的
      hideNullProvider: isHideNullProvider.value,
      qualificationIds:getListMainValue(filterServiceProviderForm.value.qualificationManagement)
    }
  })
  
  return {
    serviceProviderListParams
  }
}

/** 
 * @description 选人组件 服务商人员列表参数
*/
function useBaseSelectUserServiceProviderUserListParams() {
  
  const { 
    serviceProviderUserListParams: originServiceProviderUserListParams, 
    setServiceProviderUserListParams 
  } = useBaseSelectUserStoreServiceProviderUserListParams()
  const { filterServiceProviderForm } = useBaseSelectUserStoreFilterForm()
  const { currentServiceProviderNav } = useBaseSelectUserStoreCurrentServiceProviderNav()
  
  const serviceProviderUserListParams: ComputedRef<BaseSelectUserGetServiceProviderUserListModel> = computed(() => {
    return {
      // 服务商 id
      id: currentServiceProviderNav.value?.id || '',
      // 工作状态 列表
      stateList: filterServiceProviderForm.value.stateList,
      // 页码数
      pageNum: originServiceProviderUserListParams.value.pageNum,
      // 页大小
      pageSize: originServiceProviderUserListParams.value.pageSize,
      qualificationIds:getListMainValue(filterServiceProviderForm.value.qualificationManagement)
    }
  })
  
  const setPageNum = (pageNum: number = 1) => {
    
    const newServiceProviderUserListParams = {
      ...originServiceProviderUserListParams.value,
      pageNum
    }
    
    setServiceProviderUserListParams(newServiceProviderUserListParams)
  }
  
  const addPageNum = () => {
    const nextPageNum = originServiceProviderUserListParams.value.pageNum + 1
    setPageNum(nextPageNum)
  }
  
  const resetPageNum = () => {
    setPageNum()
  }
  
  return {
    serviceProviderUserListParams,
    addPageNum,
    resetPageNum
  }
}

/** 
 * @description 选人组件 查询搜索更多用户列表参数
*/
function useBaseSelectUserSearchMoreUserListParams() {
  
  const { 
    searchMoreUserListParams: originSearchMoreUserListParams, 
    setSearchMoreUserListParams 
  } = useBaseSelectUserStoreSearchMoreUserListParams()
  
  const { keyword } = useBaseSelectUserStoreSearchKeyword()
  const { currentSearchMoreType } = useBaseSelectUserStoreCurrentSearchMoreType()
  
  const { showDelete, showTagIfSpUser, showNoDistribute } = useBaseSelectUserOptions()
  
  const searchMoreUserListParams: ComputedRef<BaseSelectUserGetSearchMoreUserListModel> = computed(() => {
    return {
      // 搜索更多类型
      clickType: currentSearchMoreType?.value || BaseSelectUserSearchMoreTypeEnum.Organization,
      // 搜索关键字
      keyword: keyword.value,
      // 页码数
      pageNum: originSearchMoreUserListParams.value.pageNum,
      // 页大小
      pageSize: originSearchMoreUserListParams.value.pageSize,
      // 是否显示已删除人员
      showDelete: showDelete.value,
      // 当前用户为服务商成员时，是否显示 部门用户数据
      showTagIfSpUser: showTagIfSpUser.value,
      // 是否显示待分配的人员
      showNoDistribute: showNoDistribute.value
    }
  })
  
  const setPageNum = (pageNum: number = 1) => {
    
    const newSearchMoreUserListParams = {
      ...originSearchMoreUserListParams.value,
      pageNum
    }
    
    setSearchMoreUserListParams(newSearchMoreUserListParams)
  }
  
  const addPageNum = () => {
    const nextPageNum = originSearchMoreUserListParams.value.pageNum + 1
    setPageNum(nextPageNum)
  }
  
  const resetPageNum = () => {
    setPageNum()
  }
  
  return {
    searchMoreUserListParams,
    addPageNum,
    resetPageNum
  }
}

export {
  useBaseSelectUserDepartmentListParams,
  useBaseSelectUserDepartmentUserListParams,
  useBaseSelectUserRoleListParams,
  useBaseSelectUserRoleUserListParams,
  useBaseSelectUserServiceProviderListParams,
  useBaseSelectUserServiceProviderUserListParams,
  useBaseSelectUserSearchMoreUserListParams
}
